<template>
  <div class="home">
    <div class="head">
      <div>
        <img src="../../assets/images/logo.png" alt="" class="logo"/>
      </div>
      <div class="btnlist" v-show="!store.state.token">
        <button class="login" @click="$router.push('/Login')">登录</button>
        <button class="reg" @click="$router.push('/Register')">注册</button>
      </div>
      <div class="user-msg" v-show="store.state.token">
        <div>
          <p class="u-phone">{{store.state.userMsg.mobile}}</p>
          <p class="u-money">{{store.state.userMsg.money}}</p>
        </div>
        <img @click="changeRouter('/User')" :src="store.state.userMsg.photo" alt="">
      </div>
    </div>
    <div class="notice">
      <div class="notib">
        <van-notice-bar
          color="#939CAB"
          :text="noticeContent"
        />
      </div>
    </div>
    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" :show-indicators='false' indicator-color="white">
        <van-swipe-item v-for="img in imgRef" :key="img.id">
          <img :src="img.image" alt="">
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="menu">
      <ul>
        <template v-for="(item, index) in menu" :key="index">
          <li @click="changeRouter(item.path)">
            <img :src="item.img" alt="" />
            <p>{{ item.name }}</p>
          </li>
        </template>
      </ul>
    </div>
    <HomeList :boxType="2" title="武器箱" color="#23E3FA" />
    <HomeList :boxType="1" title="最新上架" color="#FFCF2A" />
    <HomeList :boxType="3" title="手套箱" color="#05F22C" />
    <HomeList :boxType="4" title="最强火力" color="#F24205" />
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useStore } from "vuex";
import { onMounted, reactive, toRefs, ref, inject } from "vue";
import HomeList from '../../components/homelist.vue'
import { notice, Banner } from '../../api/home.js'
import backpack from '../../assets/images/beibao.png';
import pay from '../../assets/images/dzhifu.png';
import lukey from '../../assets/images/xingyunri.png';
import skin from '../../assets/images/duizhan.png';
import fuli from '../../assets/images/hongbao.png';
import roll from '../../assets/images/lihe.png';
import stores from '../../assets/images/shangcheng.png';
import chat from '../../assets/images/liaotian.png';
import service from '../../assets/images/kefu.png';
const useData = reactive({
  imgRef: [],
  notice: [],
  classOption: {
    direction: 2
  }
});

const router = useRouter()

const store = useStore()

const menu = reactive([
  {
    name: '背包',
    img: backpack,
    path: '/Knapsack'
  },
  {
    name: '支付',
    img: pay,
    path: '/'
  },
  {
    name: '幸运饰品',
    img: lukey,
    path: '/Luck'
  },
  {
    name: '盲盒对战',
    img: skin,
    path: '/'
  },
  {
    name: '福利',
    img: fuli,
    path: '/'
  },
  {
    name: 'ROLL房',
    img: roll,
    path: '/'
  },
  {
    name: '商城',
    img: stores,
    path: '/'
  },
  {
    name: '官方群',
    img: chat,
    path: '/'
  },
  {
    name: '客服',
    img: service,
    path: '/'
  }
])

const changeRouter = (query) => {
  router.push(query)
}

let noticeContent = ref('')
const { imgRef } = { ... toRefs(useData) };
const getBanner = async () => {
  // console.log(this)
  const { data } = await Banner();
  imgRef.value = data;
};
const getNotice = async () => {
  const res = await notice()
  if (res.code === 200) {
    let str = ''
    res.data.forEach(ele => {
      str += ele.context
    })
    noticeContent.value = str
  }
}

onMounted(() => {
  getBanner()
  getNotice()
  console.log(inject('testgloble'))
});
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  background: #141a28;
}
.head {
  height: 92px;
  display: flex;
  align-items: center;
  background: #000000;
  justify-content: space-between;
  padding: 0 35px;
  .btnlist {
    button {
      width: 99px;
      height: 55px;
      background: transparent;
      &.login {
        margin-right: 27px;
        border: 2px solid #ffffff;
        color: #fff;
      }
      &.reg {
        color: #039dca;
        border: 2px solid #039dca;
      }
    }
  }
}

.notice {
  height: 76px;
  background: #141a28;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: -1px;
  .notib {
    width: 711px;
    height: 38px;
    line-height: 38px;
    background-image: url(../../assets/images/notice.png);
    background-repeat: no-repeat;
    background-size: 24px 22px;
    background-position: 10px 8px;
    padding-left: 30px;
    box-sizing: border-box;
    background-color: #202430;
    opacity: 0.8;
    border-radius: 19px;
    color: #939cab;
    font-size: 16px;
  }
}
.banner {
  width: 100%;
  height: 266px;
  .my-swipe{
    width: 100%;
    height: 100%;
  }
  img {
    width: 100%;
    display: block;
    height: 100%;
  }
}
.menu {
  margin: 25px 0px;
  ul {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 20px;
    li {
      text-align: center;
      width: 20%;
      padding: 25px 0;
      p {
        color: #b4bec1;
        font-size: 24px;
        margin-bottom: 10px;
      }
    }
    img {
      height: 40px;
      display: block;
      margin: 0 auto 8px;
    }
  }
}
/deep/.van-notice-bar{
  height: 40px !important;
}
.logo{
  width: 130px;
}
.user-msg{
    display: flex;
    align-items: center;
    text-align: right;
    .u-phone{
      font-size: 22px;
      color: #939CAB
    }
    .u-money{
      font-size: 24px;
      color: #FFA22A
    }
    img{
      width: 67px;
      margin-left: 18px;
    }
  }
</style>